package main;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import data.Adjectif;
import data.Base;
import data.Gender;
import data.Prefix;
import data.Suffix;
import data.Type;

public final class IOUtil {

	public static Vector<Type> buildTypes() {
		String path = ".\\resources\\pieces.txt";
		Vector<Type> types = new Vector<Type>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] pieces.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];
			p1 = p1.trim();
			String name = p1.substring(0, p1.length() - 2);
			String gender = p1.substring(p1.length() - 2, p1.length());

			Type t = null;
			
			if (gender.equals("ms")) {
				t = new Type(name, Gender.MS);
			} else if (gender.equals("mp")) {
				t = new Type(name, Gender.MP);
			} else if (gender.equals("fs")) {
				t = new Type(name, Gender.FS);
			} else if (gender.equals("fp")) {
				t = new Type(name, Gender.FP);
			}

			constructMap(parts, t);
			types.add(t);
		}

		return types;
	}

	// PREFIXES ************************************************
	public static Vector<Prefix> buildPrefixes() {
		String path = ".\\resources\\prefixes.txt";
		Vector<Prefix> prefixes = new Vector<Prefix>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] prefixes.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];

			Prefix adj = buildPrefixFromPattern(p1.trim());
			constructMap(parts, adj);

			prefixes.add(adj);
		}

		return prefixes;
	}

	// ADJECTIFS ************************************************
	public static Vector<Adjectif> buildAdjectives() {
		String path = ".\\resources\\adjectifs.txt";
		Vector<Adjectif> adjectives = new Vector<Adjectif>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] prefixes.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];

			Adjectif adj = buildAdjectifFromPattern(p1.trim());
			constructMap(parts, adj);

			adjectives.add(adj);
		}

		return adjectives;
	}

	// ADJECTIVES FOR WEAPON ************************************************
	public static Vector<Adjectif> buildWeaponAdjectives() {
		String path = ".\\resources\\adjectifs_pieces.txt";
		Vector<Adjectif> adjectives = new Vector<Adjectif>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] prefixes.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];

			Adjectif adj = buildAdjectifFromPattern(p1.trim());
			constructMap(parts, adj);

			adjectives.add(adj);
		}

		return adjectives;
	}

	// ADJECTIVES FOR SUFFIX ************************************************
	public static Vector<Adjectif> buildSuffixAdjectives() {
		String path = ".\\resources\\adjectifs_suffixes.txt";
		Vector<Adjectif> adjectives = new Vector<Adjectif>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] prefixes.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];

			Adjectif adj = buildAdjectifFromPattern(p1.trim());
			constructMap(parts, adj);

			adjectives.add(adj);
		}

		return adjectives;
	}

	// SUFFIX ************************************************
	public static Vector<Suffix> buildSuffixes() {
		String path = ".\\resources\\suffixes.txt";
		Vector<Suffix> suffixes = new Vector<Suffix>();

		File input = new File(path);
		if (!input.exists()) {
			System.out.println("[ERROR] prefixes.txt doesn't exist.");
			return null;
		}

		Scanner scan = null;
		try {
			scan = new Scanner(input);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		scan.useDelimiter("\n");

		while (scan.hasNext()) {
			String line = scan.next();
			String[] parts = line.split(",");
			String p1 = parts[0];
			p1 = p1.trim();
			String name = p1.substring(0, p1.length() - 2);
			String gender = p1.substring(p1.length() - 2, p1.length());

			Suffix suf = null;

			if (gender.equals("ms")) {
				suf = new Suffix(name, Gender.MS);
			} else if (gender.equals("mp")) {
				suf = new Suffix(name, Gender.MP);
			} else if (gender.equals("fs")) {
				suf = new Suffix(name, Gender.FS);
			} else if (gender.equals("fp")) {
				suf = new Suffix(name, Gender.FP);
			}

			constructMap(parts, suf);
			suffixes.add(suf);
		}

		return suffixes;
	}

	private static Prefix buildPrefixFromPattern(String pattern) {
		// the pattern is like Grand[(s)]{e(s)}
		String ms = pattern.replaceAll("\\{.*\\}", "").replaceAll("\\(.*\\)",
				"").replaceAll("\\[", "").replaceAll("\\]", "");
		String mp = pattern.replaceAll("\\{.*\\}", "").replaceAll("\\(", "")
				.replaceAll("\\)", "").replaceAll("\\[", "").replaceAll("\\]",
						"");
		String fs = pattern.replaceAll("\\[.*\\]", "").replaceAll("\\(.*\\)",
				"").replaceAll("\\{", "").replaceAll("\\}", "");
		String fp = pattern.replaceAll("\\[.*\\]", "").replaceAll("\\(", "")
				.replaceAll("\\)", "").replaceAll("\\{", "").replaceAll("\\}",
						"");
		return new Prefix(ms, mp, fs, fp, "osef");
	}

	private static Adjectif buildAdjectifFromPattern(String pattern) {
		// the pattern is like Grand[(s)]{e(s)}
		String ms = pattern.replaceAll("\\{.*\\}", "").replaceAll("\\(.*\\)",
				"").replaceAll("\\[", "").replaceAll("\\]", "");
		String mp = pattern.replaceAll("\\{.*\\}", "").replaceAll("\\(", "")
				.replaceAll("\\)", "").replaceAll("\\[", "").replaceAll("\\]",
						"");
		String fs = pattern.replaceAll("\\[.*\\]", "").replaceAll("\\(.*\\)",
				"").replaceAll("\\{", "").replaceAll("\\}", "");
		String fp = pattern.replaceAll("\\[.*\\]", "").replaceAll("\\(", "")
				.replaceAll("\\)", "").replaceAll("\\{", "").replaceAll("\\}",
						"");
		return new Adjectif(ms, mp, fs, fp, "osdf");
	}

	private static void constructMap(String[] parts, Base base) {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		List<String> misc = new ArrayList<String>();

		for (int i = 1; i < parts.length; i++) {
			String stat = parts[i];

			if (!stat.contains(":"))
				continue;

			String[] statSplit = stat.split(":");
			String key = statSplit[0];
			String value = statSplit[1];

			if (key.equals("misc")) {
				misc.add(value);
			} else {
				value = value.trim();

				String modif = value.substring(0, 1);
				String rest = value.substring(1);
				rest = rest.trim();
				Integer modifier = Integer.parseInt(rest);

				if (modif.equals("-")) {
					modifier = -modifier;
				}

				if (map.containsKey(key)) {
					map.put(key, map.get(key) + modifier);
				} else {
					map.put(key, modifier);
				}
			}

			base.setCharacStats(map);
			base.setMiscStats(misc);
		}
	}
}
